package com.ck.ck_back.service.impl;

import com.ck.ck_back.dao.CommentMapper;
import com.ck.ck_back.dao.LikeCommentMapper;
import com.ck.ck_back.pojo.Comment;
import com.ck.ck_back.service.commentservice;
import com.ck.ck_back.utils.WordContext;
import com.ck.ck_back.utils.WordFilter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class commentserviceImpl implements commentservice {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private LikeCommentMapper likeCommentMapper;

    private final WordContext context = new WordContext();
    private final WordFilter filter = new WordFilter(context);

    List<Comment> tem = new ArrayList<>();

    @Override
    public List<Comment> select(int production_id) {
        if (!String.valueOf(production_id).equals("")){
            //        首先查找根评论
            List<Comment> comments = commentMapper.selectByProductionId_null(production_id);
            for (Comment c:comments
            ) {

                int id = c.getCommentId();
//            获取到根评论下的子评论
                List<Comment> commentschildren = commentMapper.select_notnull(id);

                get_all_children(commentschildren);
                c.setChildren(tem);
                tem = new ArrayList<>();
            }
            return comments;
        }else {return null;}
    }

    //    获取所有子评论
    public void get_all_children(List<Comment> comments){
        if (comments.size() >0){
            for (Comment com: comments
                 ) {
                int id = com.getCommentId();
                List<Comment> comments1 = commentMapper.select_notnull(id);
                tem.add(com);
                get_all_children(comments1);
            }
        }
    }

    @Override
    public List<Comment> getall(int production_id) {
        if (!String.valueOf(production_id).equals("")){
            List<Comment> comments = commentMapper.select_all(production_id);
            for (Comment comment:comments
                 ) {
                int new_like =likeCommentMapper.selectcomment(comment.getCommentId());
//                int like_num = comment.getCommentLike() + new_like;
                comment.setCommentLike(new_like);
                commentMapper.updateByPrimaryKeySelective(comment);
            }
            return comments;
        }else {return null;}
    }

//   插入评论，1成功，404 参数错误，2失败
    @Transactional
    @Override
    public int insert(Comment comment) {
        if (comment.getCommentContent()!=null&&comment.getCreateTime()!=null
        &&comment.getUserId()!=null&&comment.getProductionId()!=null
        ){
            String newcom = filter.replace(comment.getCommentContent());
            comment.setCommentContent(newcom);
            int result = commentMapper.insertSelective(comment);
            if(result == 1){
                return result;
            }else {return 2;}
        }else {return 404;}

    }

//    删除评论，404参数为空，3无此评论，2删除失败，1成功
    @Transactional
    @Override
    public int delete(int comment_id) {
        if (!String.valueOf(comment_id).equals("")){
            Comment comment = commentMapper.selectByPrimaryKey(comment_id);
            if (comment != null){
                comment.setIsDelete(true);
                comment.setUpdateTime(new Date().getTime());
                int result = commentMapper.updateByPrimaryKeySelective(comment);
                if (result == 1){
                    return result;
                }else {return 2;}
            }else {
                return 3;
            }
        }else {return 404;}
    }
}
